package Webpacus;

use strict;
use warnings;

#
# Set flags and add plugins for the application
#
#         -Debug: activates the debug mode for very useful log messages
# Static::Simple: will serve static files from the applications root directory
#
use Catalyst qw/-Debug
	Prototype
	Config::YAML
	FillInForm
	SubRequest
	Static::Simple
	StackTrace
	Dumper
/;
#	FormValidator

use Encode qw/is_utf8 decode/;

our $VERSION = '0.37-dev';

#
# Configure the application
#
__PACKAGE__->config(
	name => 'Webpacus',
	config_file => 'config.yml',
);

#
# Start the application
#
__PACKAGE__->setup;

=head1 NAME

Webpacus - Catalyst based application

=head1 SYNOPSIS

    script/webpacus_server.pl

=head1 DESCRIPTION

Catalyst based application.

=head1 METHODS

=over 4

=item default

=cut

sub default : Private {
    my ( $self, $c ) = @_;
    $c->stash->{template} = 'index.tt';

	my $site_name = shift @{ $c->req->args } || '';

	$c->log->debug("trying site name: $site_name");

	if (my $sites_root = $c->config->{sites_root}) {
		my $site_path = $c->path_to('root', $sites_root, $site_name);

		if (-e $site_path) {
			$c->stash->{site} = $site_name;
			if (my $action = join('/', @{ $c->req->args })) {
				$c->log->debug( "site: $site_name, action: $action" );
				# do a subrequest
				$c->res->body( $c->subreq( $action, $c->stash, @{ $c->req->args } ) );
			} else {
				$c->log->debug( "site: $site_name" );
			}
		} else {
			$c->log->debug( "can't find site $site_name in $site_path");
		}
	} else {
		$c->log->warn("sites_root not defined in config");
	}
}

=item end

=cut

sub end : Private {
    my ( $self, $c ) = @_;

    $c->stash->{webpacus_version} = $VERSION;

    if ($c->response->body) {
    	$c->log->debug("at end body exists, we won't touch it!");
    } else {
		if (my $site_name = $c->stash->{site}) {
			my $template_path = $c->path_to('root', $c->config->{sites_root}, $site_name, $c->stash->{template});
			if (-e $template_path) {
				$c->stash->{template} = $c->config->{sites_root} . "/$site_name/" . $c->stash->{template};
			} else {
				$c->log->warn("can't find $template_path, fallback to default");
			}
			$c->log->debug("using template " . $c->stash->{template});

			$c->stash->{site_url} = "/$site_name/";

			#$c->log->dumper( $c->stash, 'stash' );

		}

		# Forward to View unless response body is already defined
		$c->forward('View::TT');

		# oh, turn utf8 flag for all parametars (why do I need to do this?)
		my $utf8_params;
		map {
			$utf8_params->{$_} = decode('UTF-8', $c->request->params->{$_});
		} keys %{ $c->request->params };

		# and fill-in form with new UTF-8 encoded params (to prevent double-escape of UTF-8)
		$c->fillform( $utf8_params );

		$c->response->{body} =~ s#\Qnew Ajax.Autocompleter(\E#new WebPAC.Suggest(#gs;
		$c->response->{body} =~ s#\Qnew Ajax.Updater(\E#new WebPAC.Updater(#gs;

#		$c->log->debug( $c->response->body );
    }

}

=back

=head1 AUTHOR

Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
